package Task25;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

public class History {
	private static int index;
	private static String[] listHistory;
	
	public static void resetIndex(){
		index = 0;
		}
	
	public static String getCommand(int flag){
		readHistory();
			int num = 0;
			if (flag==0) num = listHistory.length - index-1;
			if (flag==1) num = listHistory.length - index;
			if (flag==0&&index!=listHistory.length-1) index++;
			if (flag==1&&index!=1) index--;
			return listHistory[num];
	}
	
	private static void readHistory(){
		try{
			List<String> list = new LinkedList<String>();
			FileReader history = new FileReader("log\\history.log");
			BufferedReader reader = new BufferedReader(history);
			String str = "";
			while ((str = reader.readLine()) != null) {
				str = processLineHistory(str);
				list.add(str);
				}
			reader.close();
			listHistory = new String[list.size()];
		    list.toArray(listHistory);
		    list.clear();
		 }catch (IOException e) {}
	}
	
	private static String processLineHistory(String line){
		String newLine = line.substring(7);
		return newLine;
	}
}